const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const srcRoot = path.resolve(__dirname, 'src')
const devPath = path.resolve(__dirname, 'dev')
const pageDir = path.resolve(srcRoot, 'page')
const mainFile = 'index.js'

//scr目录下的page目录
function getEntry() {
  let entryMap = {}
  //readdirSync目录文件
  fs.readdirSync(pageDir).forEach((pathname) => {
    let fullPathName = path.resolve(pageDir, pathname)
    let stat = fs.statSync(fullPathName)
    let fileName = path.resolve(fullPathName, mainFile)
    if (stat.isDirectory() && fs.existsSync(fileName)) {
      entryMap[pathname] = fileName
    }
  })
  return entryMap
}

function getHtmlArray(entryMap) {
  let htmlArray = [];
  Object.keys(entryMap).forEach((key) => {
    let fullPathName = path.resolve(pageDir, key)
    let filename = path.resolve(fullPathName, key + '.html')
    if (fs.existsSync(filename)) {
      htmlArray.push(new HtmlWebpackPlugin({
        filename: key + '.html',
        template: filename,
        chunks: [key]
      }))
    }
  })
  return htmlArray
}

const entryMap = getEntry()
const htmlArray = getHtmlArray(entryMap)
module.exports = {
  entry: entryMap,
  output: {
    path: devPath,
    filename: '[name].min.js'
  },
  //省略扩展名
  resolve: {
    extensions: ['.js', '.jsx']
},
  devServer: {
    contentBase: devPath,
    hot:true
  }
  ,
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin()
  ].concat(htmlArray),
  module: {
    rules: [
      {
        test: /\.(js|jsx)?$/, exclude: /node_modules/, use: [{loader: 'babel-loader'}, {loader: 'eslint-loader'}]
      },
      {test: /\.css$/, use: ['style-loader', 'css-loader'], include: srcRoot},
      {
        test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader', {
          loader: "sass-resources-loader",
          options: {
            resources: srcRoot + '/component/common.scss'
          }
        }], include: srcRoot
      },
      {test: /\.(png|jpg|jpeg)$/, use: 'url-loader?limit=8192', include: srcRoot}
    ]
  }
}